<template>
  <el-dialog
    v-el-drag-dialog
    title="新增模版"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="90px">
      <el-form-item label="模版类型:" prop="type">
        <el-select v-model="temp.type" style="width: 100%;" placeholder="请选择" clearable>
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.add" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { savePrinterType } from '@/api'

  export default {
    name: 'add-customize',
    data() {
      return {
        visible: false,
        typeList: [
          {
            label: '配送单',
            value: 1
          },
          {
            label: '瓶批销售单',
            value: 2
          },
          {
            label: '槽批销售单',
            value: 3
          },
          {
            label: '押金单',
            value: 15
          },
          {
            label: '调拨单',
            value: 11
          },
          {
            label: '报修单',
            value: 18
          },
          {
            label: '装卸车单',
            value: 20
          },
          {
            label: '测试单',
            value: 30
          },
          {
            label: '瓶批销售单-新版',
            value: 36
          },
          {
            label: '槽批销售单-新版',
            value: 37
          },
          {
            label: '配送单-新版',
            value: 38
          }
        ],
        temp: {
          type: ''
        },
        rules: {
          type: [{ required: true, message: '请选择模版类型', trigger: 'change' }]
        }
      }
    },
    methods: {
      open() {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          var paramInfo = {
            templateType: this.temp.type,
            templateContent: ''
          }
          savePrinterType(paramInfo).then(res => {
            if (res.exc) {
              this.$message.warning(res.exc || '保存打印信息异常!')
              return
            }
            this.resolve()
            this.visible = false
            this.$notify({
              title: '提示',
              message: '添加模版成功',
              type: 'success',
              duration: 2000
            })
          })
        })
      },
      reset() {
        this.temp = {
          type: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.autocomplete {
  display: flex;
  align-items: center;
  justify-content: space-between;
  span:nth-of-type(2) {
    color: #b4b4b4;
  }
}
</style>
